package study.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import study.models.Student;

/**
 * Servlet implementation class ImageShow
 */

public class ImageShow extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImageShow() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doCheck(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doCheck(request, response);
	}

	private void doCheck(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
//		session.invalidate();
		if (username == null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Login.jsp");
			dispatcher.forward(request, response);
		} else {
			for (Cookie c : request.getCookies()) {
				System.out.println(c.getName());
				System.out.println(c.getValue());
			}
			this.getServletContext().setAttribute("otherScope", "application scope");
			session.setAttribute("otherScope", "session scope");
			request.setAttribute("otherScope", "request scope");
			Student student = new Student();
			student.setName("hai nguyen");
			request.setAttribute("student", student);
			
			ArrayList<String> languages = new ArrayList<String>();
			languages.add("java");
			languages.add("HTML");
			languages.add("C#");
			request.setAttribute("languages", languages);
			
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/buffalo.jsp");
			dispatcher.forward(request, response);
		}
	}

}
